<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/root">using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using <xsl:value-of select="name"/>.Forms;
using CSW.Framework.UI.DevEx.Forms;

namespace <xsl:value-of select="name" /><![CDATA[
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.ThreadException += Application_ThreadException;
                Application.Run(new MainForm());
            }
            catch (Exception ex)
            {
                ShowException(ex, true);
            }
        }
        
        /// <summary>
        /// Gets the application data directory.
        /// </summary>
        /// <value>The application data directory.</value>
        public static string AppDataDirectory
        {            
            get 
            {
                return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Application.ProductName); 
            }
        }
        
        public static string ApplicationDirectory
        {
            get
            {
                return Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            }
        }        

        /// <summary>
        /// Handles all uncaught exceptions on the UI thread.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Threading.ThreadExceptionEventArgs"/> instance containing the event data.</param>
        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            ShowException(e.Exception, false);
        }

        public static void ShowException(Exception e)
        {
            ShowException(e, false);
        }

        private static void ShowException(Exception e, bool occuredDuringStartup)
        {
            bool isFatal = false;

            string shortErrMsg = string.Empty;

            // Extract all inner exceptions
            Exception curException = e;
            do
            {
                if (curException.Message == "Value Dispose() cannot be called while doing CreateHandle()." ||
                    curException.Message == "Error creating window handle.")
                {
                    isFatal = true;
                }

                shortErrMsg += curException.Message + Environment.NewLine + Environment.NewLine;
                curException = curException.InnerException;
            } while (curException != null);

            // Attempt to write to the EventLog
            try
            {
                string longErrMsg = shortErrMsg + Environment.NewLine + "---" + Environment.NewLine + e.StackTrace;

                EventLog log = new EventLog();
                log.Source = Application.ProductName + " " + Application.ProductVersion;
                log.WriteEntry(longErrMsg, EventLogEntryType.Error);
            }
            catch
            {
                Trace.WriteLine("Writing to event log failed");
                // do nothing
            }

            // The Exception was most likely caused by a rapid startup/shutdown sequence.  Exit the program.
            if (isFatal)
            {
                Environment.Exit(0);
            }
            // Show Exception form
            else
            {
                ShowException(shortErrMsg, e.StackTrace, false, occuredDuringStartup);

                if (occuredDuringStartup)
                {
                    Environment.Exit(0);
                }
            }
        }

        private static void ShowException(string shortErrMsg, string stackTrace, bool isTopMost, bool occuredDuringStartup)
        {
			Cursor.Current = Cursors.WaitCursor;
            using (ExceptionForm exceptionForm = new ExceptionForm(shortErrMsg, stackTrace, isTopMost, occuredDuringStartup))
            {
				Cursor.Current = Cursors.Default;
                exceptionForm.ShowDialog();
            }
        }
    }
}
]]>
</xsl:template>
</xsl:stylesheet>